package com.ideaction.exception;

import com.ideaction.game.exception.AuthFailException;
import com.ideaction.game.exception.IllegalRequestException;
import com.ideaction.globalconst.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ZGame 处理全局异常
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    
    @ExceptionHandler(value = AuthFailException.class)
    @ResponseBody
    public String authFailException(HttpServletRequest req, AuthFailException e) {
        log.error(" 权限验证失败 uid:" + e.getUserId());
        Result r = Result.build();
        r.put("Result", false);
        r.put("Notice", e.getNotice());
        return r.response();
    }
    
    @ExceptionHandler(value = IllegalRequestException.class)
    @ResponseBody
    public String IllegalRequest(HttpServletRequest req, IllegalRequestException e) {
        Result r = Result.build();
        r.put("Result", "IllegalRequest");
        return r.response();
    }
}
